/**
 * 
 */
package SQLDAL;

import Acquaintance.IAMark;
import DALFactory.IDataMapper3;
import Entity.ELocation;
import Entity.EMark;
import Entity.ERankElement;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *SQLServer数据层负责实现IDataMapper3接口的类
 * @author Administrator
 */
public class SQLDataMapper3 implements IDataMapper3 {
/**
 * 
 * @param UID
 * @param HID
 * @param Collection
 * @return 
 */
    public boolean InsertCat(int UID, int HID, String Collection) {

        String sql = "INSERT INTO [RH].[dbo].[" + Collection + "]("
                + "UID,HID) "
                + "VALUES('" + UID
                + "','" + HID
                + "')";
        SQLStatement sqlstmt = new SQLStatement();
        SQLConnection sc = sqlstmt.openSQLConnection();
        boolean result = sqlstmt.executeUpdate(sc, sql);
        sqlstmt.closeSQLConnection(sc);
        return result;
    }
/**
 * 
 * @param UID
 * @param Collection
 * @return 
 */
    public ArrayList<Integer> getLog(int UID, String Collection) {
       ArrayList<Integer> result=new  ArrayList<Integer>();
       String sql = "SELECT * FROM [RH].[dbo].["+Collection+"] WHERE UID="+UID;
        SQLStatement sqlstmt=new SQLStatement();
        SQLConnection sc=sqlstmt.openSQLConnection();
        ResultSet rs = sqlstmt.executeQuery(sc,sql);
        try
        {
            while (rs.next())
            {
                 int HID=rs.getInt("HID");
                 result.add(HID);
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            sqlstmt.closeSQLConnection(sc);
        }
        return result;
    }
/**
 * 
 * @param mark
 * @return 
 */
    public boolean InsertMark(IAMark mark) {
         String sql = "INSERT INTO [RH].[dbo].[Mark]("
                + "UID,Context,Title,lat,lng) "
                + "VALUES('" + mark.getUID()
                + "','" + mark.getTitle()
                + "','" + mark.getLocation().getLat()
                + "','" + mark.getLocation().getLng()
                + "')";
        SQLStatement sqlstmt = new SQLStatement();
        SQLConnection sc = sqlstmt.openSQLConnection();
        boolean result = sqlstmt.executeUpdate(sc, sql);
        sqlstmt.closeSQLConnection(sc);
        return result;
    }
    /**
     * 
     * @param UID
     * @return 
     */
    public ArrayList<IAMark> getMark(int UID) {
        ArrayList<IAMark> result=new  ArrayList<IAMark>();
       String sql = "SELECT * FROM [RH].[dbo].[Mark] WHERE UID="+UID;
        SQLStatement sqlstmt=new SQLStatement();
        SQLConnection sc=sqlstmt.openSQLConnection();
        ResultSet rs = sqlstmt.executeQuery(sc,sql);
        try
        {
            while (rs.next())
            {
             IAMark mark=new EMark();
              String Context=rs.getString("Context").trim();
              String Title=rs.getString("Title").trim();
              double lat=rs.getDouble("lat");
              double lng=rs.getDouble("lng");;
              mark.setContext(Context);
              mark.setTitle(Title);
              mark.setUID(UID);
              mark.setLocation(new ELocation(lat,lng));
              result.add(mark);
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            sqlstmt.closeSQLConnection(sc);
        }
        return result;
    }
	@Override
	public int getUserCollectionByHID(int HID, String Collection) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public List<ERankElement> getUserCollectionRank(String Collection) {
		// TODO Auto-generated method stub
		return null;
	}
}
